package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.Course;
import com.lanou.entity.CourseQuery;
import com.lanou.service.CourseService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CourseController
 * @Description TODO
 * @Author cuipengwei
 * @Date 2025年08月13日 11:00
 * @Version 1.0
 */
@RestController
@RequestMapping("controller")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //查询全部
    @GetMapping
    public R getAllCourse(){
        List<Course> courses = courseService.selectAll();
        return R.ok(courses);
    }
    //分页多条件查询
    @GetMapping("page")
    public R getAllCourseByName(Course course,
                                @RequestParam(required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam(required = false,defaultValue = "5")Integer pageSize){
        PageInfo<Course> coursePageInfo = courseService.selectAllByName(course,pageNum,pageSize);
        return R.ok(coursePageInfo);
    }
    //增加(需要外键)
    @PostMapping
    public R addCourse(@RequestBody Course course){
        int i = courseService.addCourse(course);
        return R.ok(i);
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteCourse(@PathVariable("id") int id){
        Integer i = courseService.deleteCourse(id);
        return R.ok(i);
    }
    //修改
    @PutMapping
    public R updateCourse(@RequestBody Course course){
        Integer i = courseService.updateCourse(course);
        return R.ok(i);
    }
    //根据id查询
    @GetMapping("{id}")
    public R getCourseById(@PathVariable("id") Integer id){
        Course courseId = courseService.getCourseId(id);
        return R.ok(courseId);
    }

    //排序分页
    @GetMapping("sort")
    public R selectCourseBySort(String sortType,String interval,@RequestParam(required = false,defaultValue = "1") int pageNum,@RequestParam(required = false,defaultValue = "5") int pageSize){
        System.out.println(sortType+"111");
        PageInfo<Course> coursePageInfo = courseService.selectCourseBySort(sortType, interval, pageNum, pageSize);
        return R.ok(coursePageInfo);
    }
}
